package neu.soft.service.impl;

import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.DO.City;
import neu.soft.entity.DO.Province;
import neu.soft.entity.DO.ZipCode;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.mapper.RegionMapper;
import neu.soft.service.RegionService;
import neu.soft.util.RegionCodeUtil;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Service("ZIP_QUERY")
@Slf4j
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    @Override
    public ResultBean queryInfo(Map<String, Object> map) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {

        String loc = (String) map.get("loc");
        Map<String, String> resMap = RegionCodeUtil.splitLoc(loc);
        String cityName = resMap.get("cityName");
        String provinceName = resMap.get("provinceName");
        String regionName = resMap.get("regionName");
        //查询相应邮编
        ZipCode zipCode = regionMapper.findRegionInfo(provinceName, cityName, regionName);
        return ResultBean.success("邮编查询成功", zipCode);
    }

    @Override
    public List<Province> findLocInfo() {
        return regionMapper.findAllProvince();
    }

    @Override
    public City findInfo(Map<String, Object> map) {

        String provinceName = (String) map.get("provinceName");
        String cityName = (String) map.get("cityName");
        City city = regionMapper.findInfo(provinceName, cityName);
        if (city == null) {
            throw new AppException(AppExceptionCodeMsg.QUERY_FAIL);
        }
        return city;
    }

    @Override
    public ZipCode find(String loc) {
        Map<String, String> resMap = RegionCodeUtil.splitLoc(loc);
        String cityName = resMap.get("cityName");
        String provinceName = resMap.get("provinceName");
        String regionName = resMap.get("regionName");
        //查询相应邮编
        ZipCode zipCode = regionMapper.findRegionInfo(provinceName, cityName, regionName);
        return zipCode;
    }

}
